#ifndef FTPFILE_H
#define FTPFILE_H

#include <QString>
#include <QStringList>
#include <QTextStream>
#include <QDebug>

typedef union t_version
{
	quint64 int_version;
	struct t_part
	{
		quint8 v7;
		quint8 v6;
		quint8 v5;
		quint8 v4;
		quint8 v3;
		quint8 v2;
		quint8 v1;
		quint8 v0;
	} part;
	quint8 v[8];
	t_version &operator=(const t_version &o1) {this->int_version = o1.int_version; return *this;}
	t_version &operator=(const QString &o1)
	{
                this->int_version = 0;
		if (o1.isEmpty()) throw("Version string must be not empty!");
                bool ok;
                QStringList vs = o1.split(".");
                if(vs.count()>7) throw("Version string is to large!");
                for(int i=0;i<vs.count();i++)
                {
                    this->v[i] = (quint8)(vs.at(i).toInt(&ok));
                    if (!ok) throw("Invalid version string!");
                }
                return *this;
	}
} Version;

inline bool operator==(const Version &o1, const Version &o2) {return o1.int_version == o2.int_version;}
inline bool operator<(const Version &o1, const Version &o2) {return o1.int_version < o2.int_version;}
inline bool operator>(const Version &o1, const Version &o2) {return o1.int_version > o2.int_version;}
inline bool operator<=(const Version &o1, const Version &o2) {return o1.int_version <= o2.int_version;}
inline bool operator>=(const Version &o1, const Version &o2) {return o1.int_version >= o2.int_version;}
inline bool operator!=(const Version &o1, const Version &o2) {return o1.int_version != o2.int_version;}
inline QTextStream &operator<<(QTextStream &s, const Version &o)
{
        int n = 7;
        while((o.v[n]==0)&&(n>=0)) n--;
        if (n>=0)
        {
            QStringList vs;
            for(int i=0;i<=n;i++) vs.append(QObject::tr("%1").arg(o.v[i]));
            s<<vs.join(".");
        }
        else s << "0.0";
	return s;
}

class FTPFile
{
public:
	FTPFile();
	void setConfigName(const QString &s);
	void setLocalPath(const QString &s);
	void setFtpPath(const QString &s);
	void setExecuteCommand(const QString &s);
	void setUpdated(bool upd);
	void setOldVersion(Version v);
	void setNewVersion(Version v);
	const QString &getConfigName();
	const QString &getLocalPath();
	const QString &getFtpPath();
	const QString &getExecuteCommand();
	bool isUpdated();
	Version getOldVersion();
	Version getNewVersion();
private:
	QString config_name;
        QString local_path;
	QString ftp_path;
	QString execute_cmd;
        bool updated;
	Version old_version;
	Version new_version;
};

#endif // FTPFILE_H
